using ICSharpCode.TextEditor.Document;
using System;
namespace ICSharpCode.TextEditor.Actions
{
    public class ShiftTab : AbstractEditAction
    {
        private void RemoveTabs(IDocument document, ISelection selection, int y1, int y2)
        {
            document.UndoStack.StartUndoGroup();
            for(int i = y2 ; i >= y1 ; i--)
            {
                LineSegment lineSegment = document.GetLineSegment(i);
                if((i != y2 || lineSegment.Offset != selection.EndOffset) && lineSegment.Length > 0 && lineSegment.Length > 0)
                {
                    int num = 0;
                    if(document.GetCharAt(lineSegment.Offset) == '\t')
                    {
                        num = 1;
                    }
                    else
                    {
                        if(document.GetCharAt(lineSegment.Offset) == ' ')
                        {
                            int num2 = 1;
                            int indentationSize = document.TextEditorProperties.IndentationSize;
                            num2 = 1;
                            while(num2 < lineSegment.Length && document.GetCharAt(lineSegment.Offset + num2) == ' ')
                            {
                                num2++;
                            }
                            if(num2 >= indentationSize)
                            {
                                num = indentationSize;
                            }
                            else
                            {
                                if(lineSegment.Length > num2 && document.GetCharAt(lineSegment.Offset + num2) == '\t')
                                {
                                    num = num2 + 1;
                                }
                                else
                                {
                                    num = num2;
                                }
                            }
                        }
                    }
                    if(num > 0)
                    {
                        document.Remove(lineSegment.Offset, num);
                    }
                }
            }
            document.UndoStack.EndUndoGroup();
        }
        public override void Execute(TextArea textArea)
        {
            var caret = textArea.Caret;
            int indentationSize = textArea.Document.TextEditorProperties.IndentationSize;
            int column = caret.Column;

            if(textArea.SelectionManager.HasSomethingSelected)
            {

                textArea.BeginUpdate();
                var sCollection = new System.Collections.Generic.List<ISelection>(textArea.SelectionManager.SelectionCollection);
                foreach(ISelection current in sCollection)
                {
                    TextLocation start = current.StartPosition
                                , end = current.EndPosition;
                    //if(start.Line == end.Line && start.Column == 0) continue;
                    int y = start.Y;
                    int y2 = end.Y;
                    this.RemoveTabs(textArea.Document, current, y, y2);
                    textArea.Document.UpdateQueue.Clear();
                    textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, y, y2));
                    start.Column = Math.Max(0, start.Column - indentationSize);
                    end.Column = Math.Max(0, end.Column - indentationSize);
                    textArea.SelectionManager.SetSelection(start, end);
                }
                caret.Position = new TextLocation(Math.Max(0, column - indentationSize), textArea.Caret.Line);
                textArea.EndUpdate();
                textArea.Document.CommitUpdate();
                textArea.AutoClearSelection = false;
                return;
            }
            else if(column == 0) return;

            LineSegment lineSegmentForOffset = textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset);
            textArea.Document.GetText(lineSegmentForOffset.Offset, textArea.Caret.Offset - lineSegmentForOffset.Offset);


            int offset = caret.Offset;
            int num = column % indentationSize;

            textArea.BeginUpdate();
            if(num == 0)
            {
                textArea.Document.Remove(offset - indentationSize, indentationSize);
                caret.Position = new TextLocation(column - indentationSize, textArea.Caret.Line);
            }
            else
            {
                textArea.Document.Remove(offset - num, num);
                caret.Position = new TextLocation(column - num, textArea.Caret.Line);
            }
            textArea.EndUpdate();
        }
    }
}
